package com.hnevc.vx;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hnevc.vx.util.DB;


public class LoginActivity extends AppCompatActivity {
    private EditText usernameEd;
    private EditText studentidEd;
    private Button loginBtn;
    private TextView signInTv;
    private String usernameStr;
    private String studentStr;
    private CheckBox checkBox;
    private int remamberFlag = 0;
    private String password = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);
        initView();
        initEvent();
        if (sharedPreferences != null) {
            String name = sharedPreferences.getString("name", "");
            password = sharedPreferences.getString("password", "");
            remamberFlag = sharedPreferences.getInt("remeber_flag", 0);
            usernameEd.setText(name);
        }

        //确定为1获取 记住密码，打钩
        if (remamberFlag == 1) {
            checkBox.setChecked(true);
            studentidEd.setText(password);
        }
    }

    private void initView() {
        usernameEd = findViewById(R.id.username_et);
        studentidEd = findViewById(R.id.student_id_et);
        loginBtn = findViewById(R.id.login_btn);
        signInTv = findViewById(R.id.sign_in_tv);
        checkBox = findViewById(R.id.cb_mima);
    }

    private void initData() {
        usernameStr = usernameEd.getText().toString() + "";
        studentStr = studentidEd.getText().toString() + "";
    }

    private void initEvent() {
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                DB db = new DB(LoginActivity.this);
                db.loginSelect(usernameStr, studentStr);
                editor.putString("password", studentStr);
                editor.putString("name", usernameStr);
                if (checkBox.isChecked()) {
                    remamberFlag = 1;
                    editor.putInt("remeber_flag", remamberFlag);
                    editor.putString("password", studentStr);
                } else {
                    remamberFlag = 0;
                    editor.putInt("remeber_flag", remamberFlag);
                }
                editor.commit();
                Boolean result = db.loginSelect(usernameStr, studentStr);
                if (result) {
                    Intent intent = new Intent(LoginActivity.this, ShouyeActivity.class);
                    startActivity(intent);
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        signInTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, SignInActivity.class);
                startActivity(intent);
            }
        });
    }

}